package com.yh.correct.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.correct.dao.ISysRoleMapper;
import com.yh.correct.dao.ISysRolePermissionMapper;
import com.yh.correct.dao.ISysUserRoleMapper;
import com.yh.correct.model.SysRole;
import com.yh.correct.model.SysRolePermission;
import com.yh.correct.service.ISysRoleService;
/**
 * 系统角色service层接口实现类
 * @author Administrator
 *
 */
@Service
public class SysRoleServiceImpl implements ISysRoleService{
	
	/**
	 * 角色dao层接口
	 */
	@Autowired
	private ISysRoleMapper roleMapper;
	/**
	 * 用户角色dao层接口
	 */
	@Autowired
	private ISysUserRoleMapper userRoleMapper;
	/**
	 * 角色权限dao层接口
	 */
	@Autowired
	private ISysRolePermissionMapper rolePermissionMapper;
	
	/**
	 * 获取角色详情
	 * 1.角色表。2.角色权限关系表。3.权限表
	 */
	@Override
	public Map<String, Object> getSysRole(SysRole role) throws Exception {
		// TODO Auto-generated method stub
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("role", roleMapper.getSysRole(role));
		map.put("permissions", rolePermissionMapper.findPermissionListByRoleId(role.getId()));
		return map;
	}
	/**
	 * 增加角色
	 * 1.角色表。2.角色权限关系表
	 */
	@Override
	@Transactional
	public int insertSysRole(SysRole role,Integer[] perArr) throws Exception {
		// TODO Auto-generated method stub
		int i=roleMapper.insertSysRole(role);//插入角色
		if(perArr.length>0){
			List<SysRolePermission> list=new ArrayList<SysRolePermission>();
			for(Integer p:perArr){
				SysRolePermission rolePermission=new SysRolePermission();
				rolePermission.setRoleid(role.getId());
				rolePermission.setPermissionid(p);
				list.add(rolePermission);
			}
			i+=rolePermissionMapper.insertSysRolePermission(list);//插入角色权限关系
		}
		return i;
	}
	/**
	 * 删除角色
	 * 1.角色表。2.用户角色关系表。3.角色权限关系表
	 */
	@Override
	@Transactional
	public int deleteSysRole(SysRole role) throws Exception {
		// TODO Auto-generated method stub
		int i=roleMapper.deleteSysRole(role);
		i+=userRoleMapper.deleteByRoleId(role.getId());
		i+=rolePermissionMapper.deleteByRoleId(role.getId());
		return i;
	}

	/**
	 * 更新角色
	 * 1.角色表。2.角色权限表（删除，增加）
	 */
	@Override
	@Transactional
	public int updateSysRole(SysRole role,Integer[] perArr) throws Exception {
		// TODO Auto-generated method stub
		int i=roleMapper.updateSysRole(role);//更新角色信息
		i+=rolePermissionMapper.deleteByRoleId(role.getId());//删除角色权限关系
		if(perArr.length>0){
			List<SysRolePermission> list=new ArrayList<SysRolePermission>();
			for(Integer p:perArr){
				SysRolePermission rolePermission=new SysRolePermission();
				rolePermission.setRoleid(role.getId());
				rolePermission.setPermissionid(p);
				list.add(rolePermission);
			}
			i+=rolePermissionMapper.insertSysRolePermission(list);//插入角色权限关系
		}
		return i;
	}

	/**
	 * 动态条件查询角色分页
	 */
	@Override
	public PageInfo<Object> findListByCondition(Map<String, Object> condition) throws Exception {
		// TODO Auto-generated method stub
		PageHelper.startPage((int)condition.get("pageNum"), (int)condition.get("pageSize"), true);
		PageInfo<Object> pageInfo=new PageInfo<Object>(roleMapper.findListByCondition(condition));
		return pageInfo;
	}
	/**
	 * 查询全部角色
	 */
	@Override
	public List<SysRole> findAllSysRole() throws Exception {
		// TODO Auto-generated method stub
		return roleMapper.findAllSysRole();
	}

	@Override
	public List<Map<String, Object>> getAllRole() throws Exception {
		return roleMapper.getAllRole();
	}


}
